package com.woniuxy.java102vuecli.controller;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author author
 * @Create 2023-09-14 12:01
 */
@Slf4j
@RestController
@RequestMapping("/image")
public class ImageController {

    @RequestMapping("/upload")
    public ResponseResult<String> upload(MultipartFile image) throws IOException {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-chengdu.aliyuncs.com";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI4G3PvfsiMYdRM3oZ6dbU", "aWWdEn3fS4jqP5yv49XZv8SJTgpqRV");

        try {
            InputStream inputStream = image.getInputStream();
            //图片打散：所有图片放在一个目录下，文件数据量多，性能慢
            //思路1随机生成目录 a-z    a-z     /a/b        /a/c    /x/y
            //思路2日期 2023/10/26/
            DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd");
            String strDate = dateFormat.format(new Date());
            String name= strDate+"/"+UUID.randomUUID().toString()+"-"+image.getOriginalFilename();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest("woniutest103", name, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            return new ResponseResult<String>(200,"ok","https://woniutest103.oss-cn-chengdu.aliyuncs.com/"+name);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return new ResponseResult<String>(200,"ok",null);
    }
//    @RequestMapping("/upload")
//    public ResponseResult<String> upload(MultipartFile image) throws IOException {
//        String uuid= UUID.randomUUID().toString();
//        File file = new File("d:/images/" + uuid+image.getOriginalFilename());
//        image.transferTo(file);
//        return new ResponseResult<String>(200,"ok",uuid+image.getOriginalFilename());
//    }

}
